//
//  NTViewController.m
//  tabbarDemo
//
//  Created by MD101 on 14-10-8.
//  Copyright (c) 2014年 TabBarDemo. All rights reserved.
//

#import "NTViewController.h"
#import "NTButton.h"
#import "VMCIndexPageViewController.h"
#import "VMCCategoryContainerViewController.h"
#import "VMCCartModuleViewController.h"
#import "VMCMemberCenterModuleViewController.h"
#import "BaseNavigationViewController.h"
#import "VMCAcademyController.h"

@interface NTViewController (){
	
	//UIImageView *_tabBarView;//自定义的覆盖原先的tarbar的控件
	
	//NTButton * _previousBtn;//记录前一次选中的按钮
	
}

@end

@implementation NTViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
		
		self.view.backgroundColor = [UIColor grayColor];
		//        self.title = @"视图";
		// Custom initialization
	}
	return self;
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	//wsq
	for (UIView* obj in self.tabBar.subviews) {
		if (obj != _tabBarView) {//_tabBarView 应该单独封装。
			[obj removeFromSuperview];
		}
		//        if ([obj isKindOfClass:[]]) {
		//
		//        }
	}
}

- (void)viewDidLoad
{
	[super viewDidLoad];
	//self.tabBar.hidden = YES;
	//    CGFloat tabBarViewY = self.view.frame.size.height - 49;
	
	
	
	//wsq
	_tabBarView = [[UIImageView alloc]initWithFrame:self.tabBar.bounds];
	//	[_tabBarView setFrame:CGRectMake(0, tabBarViewY, 320, 49)];
	_tabBarView.userInteractionEnabled = YES;
	_tabBarView.backgroundColor = [UIColor whiteColor];
	
	//wsq
	[self.tabBar addSubview:_tabBarView];
	//1.首页
	VMCIndexPageViewController * home = [[VMCIndexPageViewController alloc]init];
	//    first.delegate = self;
	//wsq  多肽
	UINavigationController * homeNav = [[BaseNavigationViewController alloc]initWithRootViewController:home];
	//2.韩妆学院
	VMCAcademyController * school = [[VMCAcademyController alloc]init];
	UINavigationController * schoolNav = [[UINavigationController alloc]initWithRootViewController:school];
	
	//3.闪耀商城
	VMCCategoryContainerViewController * shopping = [[VMCCategoryContainerViewController alloc]init];
	UINavigationController * shoppingNav = [[UINavigationController alloc]initWithRootViewController:shopping];
	//4.购物车
	VMCCartModuleViewController * cart = [[VMCCartModuleViewController alloc]init];
	UINavigationController * cartNav = [[UINavigationController alloc]initWithRootViewController:cart];
	//5.会员中心
	VMCMemberCenterModuleViewController * my = [[VMCMemberCenterModuleViewController alloc]init];
	UINavigationController * myNavi = [[UINavigationController alloc]initWithRootViewController:my];
	
	
	self.viewControllers = [NSArray arrayWithObjects:homeNav,schoolNav,shoppingNav,cartNav,myNavi, nil];
	
	[self creatButtonWithNormalName:@"tabbar_home" andSelectName:@"tabbar_home_highlighted" andTitle:@"首页" andIndex:0];
	[self creatButtonWithNormalName:@"tabbar_school" andSelectName:@"tabbar_school_highlighted" andTitle:@"韩妆学院" andIndex:1];
	[self creatButtonWithNormalName:@"tabbar_shopping" andSelectName:@"tabbar_shopping_highlighted" andTitle:@"闪耀商城" andIndex:2];
	[self creatButtonWithNormalName:@"tabbar_cart" andSelectName:@"tabbar_cart_highlighted" andTitle:@"购物车" andIndex:3];
	[self creatButtonWithNormalName:@"tabbar_my" andSelectName:@"tabbar_my_highlighted" andTitle:@"会员中心" andIndex:4];
	NTButton * button = _tabBarView.subviews[0];
	[self changeViewController:button];
	
	self.selectedIndex = 0;
	
	// Do any additional setup after loading the view.
}

#pragma mark 创建一个按钮

- (void)creatButtonWithNormalName:(NSString *)normal andSelectName:(NSString *)selected andTitle:(NSString *)title andIndex:(int)index{
	
	NTButton * customButton = [NTButton buttonWithType:UIButtonTypeCustom];
	customButton.tag = index;
	
	CGFloat buttonW = _tabBarView.frame.size.width / 5;
	CGFloat buttonH = _tabBarView.frame.size.height;
	
	customButton.frame = CGRectMake(MyWidth/5 * index, 0, buttonW, buttonH);
	[customButton setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
	//[customButton setImage:[UIImage imageNamed:selected] forState:UIControlStateDisabled];
	//这里应该设置选中状态的图片。wsq
	[customButton setImage:[UIImage imageNamed:selected] forState:UIControlStateSelected];
	[customButton setTitle:title forState:UIControlStateNormal];
	
	[customButton addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchDown];
	
	customButton.imageView.contentMode = UIViewContentModeCenter;
	customButton.titleLabel.textAlignment = NSTextAlignmentCenter;
	customButton.titleLabel.font = [UIFont systemFontOfSize:12];
	[customButton setTitleColor:RGB(153, 153, 153) forState:UIControlStateNormal];
	[customButton setTitleColor:RGB(165, 46, 141) forState:UIControlStateSelected];
	
	[_tabBarView addSubview:customButton];
	
	if(index == 0)//设置第一个选择项。（默认选择项） wsq
	{
		_previousBtn = customButton;
		_previousBtn.selected = YES;
	}
	
}

#pragma mark 按钮被点击时调用
- (void)changeViewController:(NTButton *)sender
{
	if(self.selectedIndex != sender.tag){ //wsq®
		self.selectedIndex = sender.tag; //切换不同控制器的界面
		_previousBtn.selected = ! _previousBtn.selected;
		_previousBtn = sender;
		_previousBtn.selected = YES;
	}
}

#pragma mark 是否隐藏tabBar
//wsq
-(void)isHiddenCustomTabBarByBoolean:(BOOL)boolean{
	
	_tabBarView.hidden=boolean;
}

@end
